import { Equipment } from "@/core/cardsystem/card";
import { MinusOneHourse, PlusOneHourse } from "@/core/cardsystem/equipments/horses";
import { Shield } from "@/core/cardsystem/equipments/shields";
import { Weapons } from "@/core/cardsystem/equipments/weapons";
import { Player } from "../player";


export class EquipmentAreas {

    owner: Player

    equipmentArea: [Weapons|null, Shield|null, PlusOneHourse|null, MinusOneHourse|null]

    attackDistances: number
    defendDistances: number
    actualDistance: number

    constructor(owner: Player) {
        this.owner = owner
        this.equipmentArea = [null, null, null, null]
        this.defendDistances = 0
        this.attackDistances = 1
        this.actualDistance = 0
    }

    areaIndex(card: Equipment): number {
        let index = 0
        if (card instanceof Weapons) {
            index = Weapons.areaIndex()
        }
        if (card instanceof Shield) {
            index = Shield.areaIndex()
        }
        if (card instanceof PlusOneHourse) {
            index = PlusOneHourse.areaIndex()
        }
        if (card instanceof MinusOneHourse) {
            index = MinusOneHourse.areaIndex()
        }
        return index
    }

    equip(card: Equipment) {
        const index = this.areaIndex(card)
        const originEquip = this.equipmentArea[index]
        if (originEquip) {
            originEquip.onDrop()
        }
        this.equipmentArea[index] = card
    }

    onDrop(card: Equipment) {
        const index = this.areaIndex(card)
        if (this.hasEquip(card)) {
            this.equipmentArea[index] = null
        }
    }

    hasEquip(card: Equipment): boolean {
        const index = this.areaIndex(card)
        const eq = this.equipmentArea[index]
        return eq != null && eq.equals(card)
    }

    hasWeapon() {
        const eq = this.getWeapon()
        return eq != null
    }

    getWeapon() {
        const index = Weapons.areaIndex()
        return this.equipmentArea[index]
    }

    hasShield() {
        const eq = this.getShield()
        return eq != null
    }

    getShield() {
        const index = Shield.areaIndex()
        return this.equipmentArea[index]
    }

    hasPlusOneHourse() {
        const eq = this.getPlusOneHourse()
        return eq != null
    }

    getPlusOneHourse() {
        const index = PlusOneHourse.areaIndex()
        return this.equipmentArea[index]
    }

    hasMinusOneHourse() {
        const eq = this.getMinusOneHourse()
        return eq != null
    }

    getMinusOneHourse() {
        const index = MinusOneHourse.areaIndex()
        return this.equipmentArea[index]
    }

    hasEquipment(): boolean {
        const [a, b, c, d] = this.equipmentArea
        const e = a || b || c || d
        return e != null
    }

    randSelectEquipment() {
        const [a, b, c, d] = this.equipmentArea
        return a || b || c || d
    }


}
